package com.lyfam.annotation.builder;

import com.vaadin.ui.Button;
import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

public class UIBeanFormWizard
{
    private Form form;
    private Panel panel;
    private Button prev;
    private Button next;
    private int cursor;
    private String[] layouts;

    public UIBeanFormWizard(Form form, String[] layouts)
    {
        this.form = form;

        this.layouts = layouts;

        this.panel = new Panel();

        this.cursor = 0;

        this.init();
    }

    private void init()
    {
        HorizontalLayout honLay = new HorizontalLayout();
        panel.addComponent(honLay);

        prev = new Button("Previous");
        next = new Button("Next");
        prev.addListener(this.prevCLick);
        next.addListener(this.nextCLick);
        prev.setEnabled(this.hasPrevious());
        next.setEnabled(this.hasNext());

        honLay.addComponent(prev);
        honLay.addComponent(next);

        form.setLayout(new CustomLayout(layouts[0]));

        panel.addComponent(form);
    }

    private boolean hasNext()
    {
        return (this.cursor < layouts.length - 1);
    }

    private boolean hasPrevious()
    {
        return (this.cursor > 0);
    }

    private ClickListener prevCLick = new ClickListener()
    {
        @Override
        public void buttonClick(ClickEvent event)
        {
            if (hasPrevious())
            {
                cursor--;
                Layout l = new CustomLayout(layouts[cursor]);
                form.setLayout(l);
                
                prev.setEnabled(hasPrevious());
                next.setEnabled(hasNext());
            }
        }
    };

    private ClickListener nextCLick = new ClickListener()
    {
        @Override
        public void buttonClick(ClickEvent event)
        {
            if (hasNext())
            {
                cursor++;
                Layout l = new CustomLayout(layouts[cursor]);
                form.setLayout(l);
                
                prev.setEnabled(hasPrevious());
                next.setEnabled(hasNext());
            }
        }
    };

    public Panel getPanel()
    {
        return panel;
    }
}
